/**************************************************************************************************
Filename:       nb_u8g2_fps.c
Editor:         Tome @ newbit
Revised:        $Date: 2018/7/27 11:20:02 +0800  $
Revision:       $Revision: 00001 $

Description:    
History:        
Notes:          share on github.com/newbitstudio
support on bbs.newbitstudio.com

**************************************************************************************************/



/**************************************************************************************************
// INCLUDES
**************************************************************************************************/
#include "nb_u8g2api.h"
#include "hal_types.h"

#include "osal.h"
#include "osal_clock.h"


/**************************************************************************************************
// TYPEDEF
**************************************************************************************************/



/**************************************************************************************************
// CONSTANTS
**************************************************************************************************/



/**************************************************************************************************
// LOCAL VERIABLE
**************************************************************************************************/

static uint8 flip_color = 0;
static uint8 draw_color = 1;

/**************************************************************************************************
// FUNCTIONS DECLERATION
**************************************************************************************************/
void show_result(const char *s, uint16 fps);


/**************************************************************************************************
// FUNCTIONS
**************************************************************************************************/
/**************************************************************************************************
* @fn      draw_set_screen
*
* @brief   -
*          -
*
* @param   void
*          -
*          -
*
* @return  void
**************************************************************************************************/
void draw_set_screen(void)
{
  U8G2_SetDrawColor(flip_color);
  
  U8G2_DrawBox( 0, 0, U8G2_GetDisplayWidth(), U8G2_GetDisplayHeight() );
}


/**************************************************************************************************
* @fn      draw_clip_test
*
* @brief   -
*          -
*
* @param   void
*          -
*          -
*
* @return  void
**************************************************************************************************/
void draw_clip_test(void)
{
  uint8 i,j,k;
  char buf[3] = "AB";
  k = 0;
  
  U8G2_SetDrawColor(draw_color);
  U8G2_SetFont(u8g2_font_6x10_tf);
  
  for ( i = 0; i < 6; i++ )
  {
    for (j = 1; j < 8; j++ )
    {
      U8G2_DrawHLine(i-3, k,j);
      U8G2_DrawHLine(i-3+10, k, j);
      U8G2_DrawVLine(k+20, i-3, j);
      U8G2_DrawVLine(k+20, i-3+10, j);      
      k++;
    }
  }
  
  U8G2_SetFontDirection(0);
  U8G2_DrawStr(3, 50, buf);
  U8G2_SetFontDirection(2);
  U8G2_DrawStr(0+3, 50, buf);
  
  U8G2_SetFontDirection(0);
  U8G2_DrawStr(U8G2_GetDisplayWidth()-3, 40, buf);
  U8G2_SetFontDirection(2);
  U8G2_DrawStr(U8G2_GetDisplayWidth()+3, 40, buf);
  
  U8G2_SetFontDirection(1);
  U8G2_DrawStr(U8G2_GetDisplayWidth()-10, 0+3, buf);
  U8G2_SetFontDirection(3);
  U8G2_DrawStr(U8G2_GetDisplayWidth()-10, 3, buf);
  
  U8G2_SetFontDirection(1);
  U8G2_DrawStr(U8G2_GetDisplayWidth()-20, U8G2_GetDisplayHeight()+3, buf);
  U8G2_SetFontDirection(3);
  U8G2_DrawStr(U8G2_GetDisplayWidth()-20, U8G2_GetDisplayHeight()+3, buf);
  
  
  U8G2_SetFontDirection(0);
}

/**************************************************************************************************
* @fn      draw_char
*
* @brief   -
*          -
*
* @param   void
*          -
*          -
*
* @return  void
**************************************************************************************************/
void draw_char(void)
{
  char buf[2] = "@";
  
  uint8 i,j;
  
  U8G2_SetDrawColor(draw_color);
  U8G2_SetFont(u8g2_font_6x10_tf);
  
  j = 8;
  for(;;)
  {
    i = 0;
    for (;;)
    {
      U8G2_DrawStr(i,j,buf);
      i += 8;
      if ( i > U8G2_GetDisplayWidth())
        break;
    }
    j += 8;
    if ( j > U8G2_GetDisplayHeight())
      break;
  }
}


/**************************************************************************************************
* @fn      draw_pixel
*
* @brief   -
*          -
*
* @param   void
*          -
*          -
*
* @return  void
**************************************************************************************************/
void draw_pixel(void)
{
  uint8 x,y,w2,h2;
  U8G2_SetDrawColor(draw_color);
  w2 = U8G2_GetDisplayWidth();
  h2 = U8G2_GetDisplayHeight();
  
  w2 /= 2;
  h2 /= 2;
  
  for ( y = 0; y < h2; y++ ) 
  {
    for ( x = 0; x < w2; x++ )
    {
      if (( x+y) & 1)
      {
        U8G2_DrawPixel(x,y);
        U8G2_DrawPixel(x,y+h2);
        U8G2_DrawPixel(x+w2,y);
        U8G2_DrawPixel(x+w2,y+h2);
      }
    }
  }
}


/**************************************************************************************************
* @fn      draw_line
*
* @brief   -
*          -
*
* @param   void
*          -
*          -
*
* @return  void
**************************************************************************************************/
void draw_line(void)
{
  U8G2_SetDrawColor(draw_color);
  U8G2_DrawLine( 0, 0, U8G2_GetDisplayWidth()-1,
                 U8G2_GetDisplayHeight() - 1);
}


/**************************************************************************************************
* @fn      execute_with_fps
*
* @brief   -
*          -
*
* @param   void (*draw_fn)(void)
*          -
*          -
*
* @return  uint16
**************************************************************************************************/
uint16 execute_with_fps(void (*draw_fn)(void))
{
  uint16 FPS10 = 0;
  uint32 time;
  
  time = osal_getClock() + 1;
  
  do {
    U8G2_ClearBuffer();
    draw_fn();
    U8G2_SendBuffer();
    FPS10++;
    flip_color = flip_color ^ 1;

  }while(osal_getClock() < time );
  
  return  FPS10;
}


/**************************************************************************************************
* @fn      osal_execute_fps
*
* @brief   -
*          -
*
* @param   state , 1- runing, 0 display
*          -
*          -
*
* @return  void
**************************************************************************************************/
  static uint16 fps = 0;
  static uint16 fpsflw = 0;
  static uint8 test_no , test_flw;
void osal_execute_fps(uint8 drawing, uint8 move)
{

  //typedef void (*halUARTCBack_t) (uint8 port, uint8 event);
  typedef void (*func)(void);
  func draw_fn[10] = 
  
  //void (*draw_fn)[10](void) = 
  {
    draw_clip_test,
    draw_set_screen,
    draw_char,
    draw_pixel,
    draw_line,
  };
  
  char *title[] = 
  {
    "draw clip test",
    "clear screen",
    "draw @",
    "draw pixel",
    "draw line",
  };
  
  /*
  while(1)
  {
    U8G2_ClearBuffer();
    draw_set_screen();
    U8G2_SendBuffer();
    flip_color = flip_color ^ 1;
  }
  */
  if ( move )
  {
    
    fpsflw = fps;
    
    fps = 0;
    test_flw = test_no;
    test_no++;
    if ( test_no > 4) 
      test_no = 0;  

  }  
  
  if ( drawing == 1)
  {
    U8G2_ClearBuffer();
    (draw_fn[test_no])();
    U8G2_SendBuffer();
    fps ++;
    flip_color = flip_color ^ 1;
  }
  else
  {
    show_result(title[test_flw], fpsflw);
    
  }
  

}





char* convert_FPS(uint16 fps)
{
  char *buf = osal_mem_alloc(6);
  if ( buf == NULL )
    return buf;
  uint8 val = fps;
  
  buf[0] = fps / 100 + '0';
  val %= 100;
  buf[1] = fps / 10 + '0';
  
  //buf[3] = '.';
  val %= 10;
  buf[2] = (val) + '0';
  buf[3] = '\0';
  return buf;
}
/**************************************************************************************************
* @fn      show_result
*
* @brief   -
*          -
*
* @param   const char *s, uint16 fps
*          -
*          -
*
* @return  void
**************************************************************************************************/
void show_result(const char *s, uint16 fps)
{
  U8G2_SetDrawColor(draw_color);
  U8G2_SetFont(u8g2_font_8x13B_tf);
  
  char *str = convert_FPS(fps);
  
  U8G2_ClearBuffer();
  
  U8G2_DrawStr(0, 12, s);
  U8G2_DrawStr(0, 24, str);
  
  U8G2_SendBuffer();
  
  if ( str )
    osal_mem_free(str);

  
}















/**************************************************************************************************
Copyright 2016-2018 Newbit Studio. All rights reserved.
**************************************************************************************************/


